package com.soul.server.server;

import com.soul.autoconfig.template.HuanXinTemplate;
import com.soul.commons.exception.SoulGlobalException;
import com.soul.dubbo.api.db.UserApi;
import com.soul.dubbo.api.db.UserInfoApi;
import com.soul.dubbo.api.mongo.ContactApi;
import com.soul.dubbo.api.mongo.FriendApi;
import com.soul.model.db.User;
import com.soul.model.db.UserInfo;
import com.soul.model.mongo.Friend;
import com.soul.model.vo.ContactVo;
import com.soul.model.vo.ErrorResult;
import com.soul.model.vo.PageResult;
import com.soul.model.vo.UserInfoVo;
import com.soul.server.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author mdoya
 * @version 1.0
 * @description:
 */
@Slf4j
@Service
public class MessagesService {

    @DubboReference
    private UserApi userApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private FriendApi friendApi;

    @DubboReference
    private ContactApi contactApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    /**
     * 通过环信id查询用户信息
     *
     * @param huanxinId 环信用户名
     * @return
     */
    public UserInfoVo findUserInfo(String huanxinId) {
        User user = UserHolder.getUser();
        UserInfo userInfo = userInfoApi.findByUserIdUser(user.getId());
        UserInfoVo userInfoVo = null;
        if (userInfo != null) {
            userInfoVo = new UserInfoVo();
            BeanUtils.copyProperties(userInfoVo, userInfoVo);
            userInfoVo.setAge(userInfo.getAge() + "");
        }
        return userInfoVo;
    }

    /**
     *  添加联系人
     * @param friendUserId 佳人id
     * @return null
     */
    public void addContacts(Long friendUserId) {
        // 1. 获取当前登录用户信息
        User user = UserHolder.getUser();

        // 2. 佳人用户信息
        User friendUser = userApi.findById(friendUserId);

        // 3. 添加环信-即时通讯  添加好友关系
        if (!huanXinTemplate.addContact(user.getHxUser(),friendUser.getHxUser())){
            throw new SoulGlobalException(ErrorResult.error());
        }
        // 3.2 添加好友关系
        friendApi.addContacts(friendUserId,user.getId());

    }

    /**
     *  联系人列表查询分页
     * @param page 每页
     * @param pagesize 每页条数
     * @param keyword 关键字
     * @return ResponseEntity
     */
    public PageResult<ContactVo> findpageContacts(Long page, Long pagesize, String keyword) {
        // 1. 获取当前登录用户对象
        User user = UserHolder.getUser();
        // 2. 联系人列表分页
        PageResult<Friend> friendPageResult = contactApi.findpageContacts(page,pagesize,user.getId());
        if (CollectionUtils.isEmpty(friendPageResult.getItems())){
            return null;
        }
        // 3. 拿到所有好友ids
        List<Friend> friendList = friendPageResult.getItems();
        List<Long> friendIdsList = friendList.stream().map(Friend::getFriendId).collect(Collectors.toList());

        // 4. 拿到好友用户对象信息并且有条件查询
        UserInfo userInfoCondition = new UserInfo();
        userInfoCondition.setNickname(keyword);
        Map<Long, UserInfo> userInfoMap = userInfoApi.findByIdsAndCondition(friendIdsList,userInfoCondition);

        // 5. 封装数据
        List<ContactVo> contactVoList = new ArrayList<>();
        for (Friend friend : friendList) {
            if (userInfoMap != null) {
                UserInfo userInfo = userInfoMap.get(friend.getFriendId());
                if (userInfo != null){
                    contactVoList.add(ContactVo.init(userInfo));
                }
            }
        }
        return new PageResult<>(page,pagesize,friendPageResult.getCounts(),contactVoList);
    }
}
